<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/
// no direct access
defined('_JEXEC') or die('Restricted access');

class JVframeworkControllerDefault extends JVController {
   
    function __construct($default = array()) {
        $this->_setModel('default');
        $this->_setLink('');
                
        parent::__construct($default);
        $this->registerTask('default','setDefault');
    }
    
    function display(){
        parent::display();
    }
    
    function edit(){
       parent::displays();
    }
    
    function upload(){
       parent::displays('default','upload');
    }
    
    function getGoogleFont(){
        if(!$this->_model -> writeFontLists()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        
        $id = $this->_model->getId(); 
        $this->setRedirect( $this->_link.'&task=edit&cid[]='.$id, $msg );
    }
    
    function duplicate(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        if(!$this->_model -> duplicate()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
    
        $this->setRedirect( $this->_link, $msg );
    }
    
    function uploadfile(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        if(!$this->_model -> upload()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = 'Upload successfully, reload page to see new theme !<script>window.parent.location.reload()</script>';
        }    
        $this->setRedirect( $this->_link.'&task=upload&tmpl=component', $msg );
    }
    
    function reset(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        if(!$this->_model -> reset()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
    
        $id = $this->_model->getId(); 
        $this->setRedirect( $this->_link.'&task=edit&cid[]='.$id, $msg );
    }
    
    function transfer(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        if(!$this->_model -> transferfile()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
    
        $this->setRedirect( $this->_link, $msg );
    }
    
    function setDefault(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        if(!$this->_model -> makeDefault()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
    
        $this->setRedirect( $this->_link, $msg );
    }
    
    function setdefaulttmpl(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        if(!$this->_model -> setDefault()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
    
        $this->setRedirect( $this->_link, $msg );
    }
    
    function clear(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );        
        if(!$this->_model -> clearCache()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        
        $id = $this->_model->getId(); 
        $this->setRedirect( $this->_link.'&task=edit&cid[]='.$id, $msg );
    }
    
    function loadlayout(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );        
        if(!$this->_model -> loadLayout()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        
        $id = $this->_model->getId(); 
        $this->setRedirect( $this->_link.'&task=edit&cid[]='.$id, $msg );
    }
    
    function savelayout(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );        
        if(!$this->_model -> saveLayout()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        
        $id = $this->_model->getId(); 
        $this->setRedirect( $this->_link.'&task=edit&cid[]='.$id, $msg );
    }
    
    function deletelayout(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );        
        if(!$this->_model -> deleteLayout()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        
        $id = $this->_model->getId(); 
        $this->setRedirect( $this->_link.'&task=edit&cid[]='.$id, $msg );
    }
        
    function save(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );
        $task = JRequest::getVar('task');
        $data = JRequest::get('post');
        
        if(!$this->_model -> store($data)){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        $id = $this->_model->getId(); 
        
        $link = $task == 'save' ? $this->_link : $this->_link.'&task=edit&cid[]='.$id;     
        $this->setRedirect( $link, $msg );
    }
    
    function active(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );        
        if(!$this->_model -> doActive()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        $this->setRedirect( $this->_link, $msg );
    }
    
    function deactive(){
        JRequest::checkToken () or jexit ( 'Invalid Token' );        
        if(!$this->_model -> doDeactive()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        $this->setRedirect( $this->_link, $msg );
    }
    
    function loadfile(){
        if(!$this->_model -> readThemeFile()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = '';
        }
        die($msg);
    }
    
    function savefile(){
        if(!$this->_model -> saveThemeFile()){
            $msg = ERROR_MESSAGE;
        }else{
            $msg = SAVE_MESSAGE;
        }
        die($msg);
    }
}
